package com.mall.controller.interceptor;


import com.mall.domain.FeedBack;
import com.mall.domain.Page;
import com.mall.service.impl.FeedBackService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器（用户留言controller）
 * </p>
 *
 * @since 2023-12-19
 */
@RestController
@RequestMapping("/feedBack")
@RequiredArgsConstructor
public class FeedBackController {

    @Resource
    FeedBackService feedBackService;

    @PostMapping("/listFeedBackByUser")
    public @ResponseBody
    Map<String, Object> listFeedBackByUser(@RequestBody Page page) {
        //放入Map集合
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        List<FeedBack> msAnnouncements = feedBackService.listFeedBackByUser(page);
        Integer total = feedBackService.findTotal(page);
        metaMap.put("data", msAnnouncements);
        metaMap.put("total", total);
        metaMap.put("msg","获取列表成功");
        metaMap.put("status",200);
        return metaMap;
    }

    @PostMapping("/addFeedBack")
    public @ResponseBody
    Map<String, Object> addFeedBack(@RequestBody FeedBack feedBack) {
        Map<String,Object> metaMap = new HashMap<>();
        try {
            feedBackService.addFeedBack(feedBack);
            metaMap.put("msg","添加成功");
            metaMap.put("status",200);
            return metaMap;
        }catch (Exception e){
            metaMap.put("status",100);
            metaMap.put("msg",e.getMessage());
            return metaMap;
        }
    }

    @PostMapping("/delFeedBack")
    public @ResponseBody
    Map<String, Object> delFeedBack(@RequestBody FeedBack feedBack) {
        Map<String,Object> metaMap = new HashMap<>();
        try {
            feedBackService.delFeedBack(feedBack.getId());
            metaMap.put("msg","成功");
            metaMap.put("status",200);
            return metaMap;
        }catch (Exception e){
            metaMap.put("msg",e.getMessage());
            metaMap.put("status",100);
            return metaMap;
        }
    }
}
